package com.github.prontera.service;

import java.text.MessageFormat;
import java.time.OffsetDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.domain.ClientBankCard;
import com.github.prontera.domain.enums.ClientBankCardStatusEnum;
import com.github.prontera.domain.enums.ConsumeLevelTypeEnum;
import com.github.prontera.domain.enums.ConsumeSmallTypeEnum;
import com.github.prontera.domain.enums.ConsumeTypeEnum;
import com.github.prontera.domain.enums.YesOrNoEnum;
import com.github.prontera.model.ConsumeMessageTemplate;
import com.github.prontera.persistence.ClientBankCardMapper;
import com.github.prontera.persistence.CrudMapper;
import com.google.common.base.Preconditions;

/**
 * 用户银行卡列表
 */
@Service
public class ClientBankCardService extends CrudServiceImpl<ClientBankCard>{
    
	@Autowired
	private ClientBankCardMapper mapper;
	@Autowired
	private ConsumeService consumeService;
	@Autowired
	private ClientService clientService;
	
  	@Autowired
    public ClientBankCardService(CrudMapper<ClientBankCard> mapper) {
        super(mapper);
    }
  	
  	/**
  	 * 添加卡
  	 */
  	@Transactional(propagation = Propagation.REQUIRED)
  	public boolean insertCard(ClientBankCard bankCard) {
  		Preconditions.checkNotNull(bankCard.getCardNo(), "persisting cardNo should not be NULL");
  		
  		ClientBankCard findClientBankCard=new ClientBankCard();
  		findClientBankCard.setCardType(bankCard.getCardType());
  		findClientBankCard.setCardNo(bankCard.getCardNo());
  		findClientBankCard.setStatus(ClientBankCardStatusEnum.ACTIVITY);
  		if(mapper.count(findClientBankCard)>0){
  			Shift.fatal(StatusCode.CARD_BOUND, "该卡已经被绑定过");
  		}
  		findClientBankCard=new ClientBankCard();
  		findClientBankCard.setClientId(bankCard.getClientId());
  		findClientBankCard.setCardType(bankCard.getCardType());
  		findClientBankCard.setStatus(ClientBankCardStatusEnum.ACTIVITY);
  		if(mapper.count(findClientBankCard)>0){
  			Shift.fatal(StatusCode.BOUND_EQUAL_CARD, "已绑定相同类型账户");
  		}
  		if(mapper.insert(bankCard)<=0){
  			Shift.fatal(StatusCode.BOUND_EQUAL_CARD, "已绑定相同类型账户");
  		}
  		
  		clientService.updateMyBean(bankCard.getClientId(), 50);
		//自己得消费记录
		consumeService.insertConsume(bankCard.getClientId(),ConsumeSmallTypeEnum.BINDES_CARD,MessageFormat.format(ConsumeMessageTemplate.MSG_BINDED_CARD, 50) ,ConsumeLevelTypeEnum.BEAN, 50, null,ConsumeTypeEnum.INCOME,null);
  		
  		if(bankCard.getStatus()==ClientBankCardStatusEnum.ACTIVITY){
  			return defaultCard(bankCard);
  		}
  		return true;
	}
  	
  	/**
  	 * 设置默认卡
  	 */
  	@Transactional(propagation = Propagation.REQUIRED)
  	public boolean defaultCard(ClientBankCard bankCard) {
  		Preconditions.checkNotNull(bankCard.getId(), "persisting id should not be NULL");
  		
  		ClientBankCard findClientBankCard=mapper.get(bankCard.getId());
  		if(findClientBankCard.getClientId().longValue()!=bankCard.getClientId().longValue()){
  			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "只能操作账户卡");
  		}
  		ClientBankCard updateClientBankCard=new ClientBankCard();
  		updateClientBankCard.setClientId(bankCard.getClientId());
  		updateClientBankCard.setIsDefault(YesOrNoEnum.NO);
  		if(mapper.updateBankCard(updateClientBankCard)<=0){
  			Shift.fatal(StatusCode.SERVER_IS_BUSY_NOW, "绑卡失败");
  		}
  		updateClientBankCard=new ClientBankCard();
   		updateClientBankCard.setId(bankCard.getId());
   		updateClientBankCard.setIsDefault(YesOrNoEnum.YES);
  		return mapper.update(bankCard)>0;
	}
  	
  	/**
  	 * 解绑卡
  	 */
  	@Transactional(propagation = Propagation.REQUIRED)
  	public boolean removeCard(ClientBankCard bankCard) {
  		Preconditions.checkNotNull(bankCard.getId(), "persisting id should not be NULL");
  		
  		ClientBankCard findClientBankCard=mapper.get(bankCard.getId());
  		if(findClientBankCard.getClientId().longValue()!=bankCard.getClientId().longValue()){
  			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "只能操作账户卡");
  		}
  		ClientBankCard	updateClientBankCard=new ClientBankCard();
   		updateClientBankCard.setId(bankCard.getId());
   		updateClientBankCard.setDeleteTime(OffsetDateTime.now());
   		updateClientBankCard.setStatus(ClientBankCardStatusEnum.DELETED);
  		return mapper.updateBankCard(bankCard)>0;
	}
  	

}
